package utwente.hmi.touchmol;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.border.Border;

import utwente.hmi.touchmol.elements.*;

import com.merl.diamondtouch.DtlibInputTframe;

/**
 * @author Jeroen Logtenberg
 */
public class ModeratorGUI extends TouchMolGUI {
	
	private static final long serialVersionUID = 1L;
	
	public static final int LOCATION_MODERATOR = 7003;
	private ModeratorPanel moderatorPanel;
	private final int moderator = USER_BLUE; //Specifies the user that has moderator status [0-4].

	@Override
	/**
	 * The moderator GUI has a vertical control bar for the moderator with 
	 * buttons to allocate turns on the left side of the application. The
	 * rest of the screen is made up of the molpanel and the bookmark panel
	 * on the right edge.
	 */
	void makeLayout(){
		
		SCALE = 5;
		
		//Cover SCALE-2 part of the screen, plus the part that the control bar covers.
		int molpanelSize = (displayWidth / SCALE * (SCALE - 2)) + (displayWidth / SCALE) - 75;
		
		jmolView = new MolPanel(molpanelSize,displayHeight);

		screens = new ScreenshotPanel((displayWidth / SCALE) - 2, displayHeight);
		
		//Control panel for central moderator.
		moderatorPanel = new ModeratorPanel(73, displayHeight, moderator);
		moderatorPanel.setVisible(true);

		getContentPane().setLayout(new BorderLayout(1,0));
		getContentPane().add(moderatorPanel, BorderLayout.WEST);
		getContentPane().add(jmolView, BorderLayout.CENTER);
		getContentPane().add(screens, BorderLayout.EAST);
		
		Border line = BorderFactory.createLineBorder(Color.white, 5);
		screens.setBorder(line);
		screens.setIgnoreRepaint(true);
	}
	
	/**
	 * Finds out in which part of the screen the user is touching.
	 * @param dtInputFrame The touch input
	 * @return Returns in which panel the center of the touch bounding box is and updates touchLocation.
	 */
	@Override
	 int determineLocation(int userID, DtlibInputTframe dtInputFrame){
		 //debug("Determined location in ModeratorGUI");
		debug("Input frame size: " + touchRect(dtInputFrame));
		 Point center = calculateCenter(touchRect(dtInputFrame));
		 
		 if(moderatorPanel.panelTouched(center)){
			 handleModeratorInput(userID, center);
			 return LOCATION_MODERATOR;
		 } else if(jmolView.panelTouched(center)){
			 return LOCATION_MOLPANEL;
		 } else if(screens.panelTouched(center)){
			 return LOCATION_SCREENSHOTPANEL;
		 } else return -1;
		 
	 }
	 
	 /**
	  * Checks that the touching user is moderator, 
	  * then processes touch input in moderator panel.
	  * @param center The center of touch.
	  */
	 private void handleModeratorInput(int userID, Point center){
		 if(userID == moderator){
			 debug("Moderator recognized");
			 UserButton res = moderatorPanel.buttonTouched(center);
			 if(res != null){
				 debug("Releasing " + currentTurn + ", allocating turn to user " + res.getUserID());
				 releaseTurn(UserButton.getButton(currentTurn));
				 takeTurn(res);
			 } else{
				 debug("No user touched.");
			 }
		 } else{
			 debug("Unauthorized touch on moderator panel by user " + userID);
		 }
	 }
	 
	@Override
	public void releaseTurn(UserButton user) {
		moderatorPanel.releaseTurn(user);
		currentTurn = -1;
	}

	@Override
	public void requestTurn(UserButton user) {
		//No requesting possible.
		
	}

	@Override
	public void takeTurn(UserButton user) {
		moderatorPanel.takeTurn(user);
		currentTurn = user.getUserID();
		
	}
}
